package com.wl156.activities.order;

import java.io.IOException;

import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.model.Car;
import com.wl156.model.Certificate;
import com.wl156.model.Good;
import com.wl156.model.WebServiceClient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class NewOrder extends Activity implements OnClickListener {

	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;

	private TextView mGoodFrom;
	private TextView mGoodTo;
	private TextView mGoodName;
	private TextView mGoodCompany;
	private TextView mGoodWeigth;
	private TextView mGoodLength;
	private TextView mGoodNeedCarType;
	private TextView mGoodNeedCarLength;
	private TextView mGoodContact;
	private TextView mGoodPhone;
	private TextView mGoodStartTime;
	private CheckBox mGoodThirdPay;
	private EditText mOrderSendContact;
	private EditText mOrderSendPhone;
	private EditText mOrderSendAddress;
	private EditText mOrderReceiveContact;
	private EditText mOrderReceivePhone;
	private EditText mOrderReceiveAddress;
	private EditText mOrderGoodTrueName;
	private EditText mOrderGoodType;
	private EditText mOrderGoodWrapMethod;
	private EditText mOrderWeigth;
	private EditText mOrderPrice;
	private EditText mOrderTimeLimit;
	private EditText mOrderContact;
	private EditText mOrderAddress;
	private EditText mOrderRemark;

	// private TextView mCarCode;
	// private TextView mCarType;
	// private TextView mCarDriverName;
	// private TextView mCarOwnerName;
	// private TextView mCarContactPhone;
	// private TextView mCarScore;
	// private TextView mCarWeight;
	// private TextView mCarLength;
	// private TextView mCarCurrentPlace;
	private CheckBox mCarThirdPay;

	private CheckBox mOrderUseRight;
	private CheckBox mOrderUseGuarantee;
	private Button mConfirm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_order_new);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		Good good = MyApp.sGoodInfo;
		Car car = MyApp.sCarInfo;

		try {
			mGoodFrom = (TextView) findViewById(R.id.good_from);
			mGoodFrom.setText(good.from);
			mGoodTo = (TextView) findViewById(R.id.good_to);
			mGoodTo.setText(good.to);
			mGoodName = (TextView) findViewById(R.id.good_name);
			mGoodName.setText(good.name);
			mGoodCompany = (TextView) findViewById(R.id.good_company);
			mGoodCompany.setText(good.company);
			mGoodWeigth = (TextView) findViewById(R.id.good_weigth);
			mGoodWeigth.setText(good.weight);
			mGoodLength = (TextView) findViewById(R.id.good_length);
			mGoodLength.setText(good.length);
			mGoodNeedCarType = (TextView) findViewById(R.id.good_need_car_type);
			mGoodNeedCarType.setText(good.carType);
			mGoodNeedCarLength = (TextView) findViewById(R.id.good_need_car_length);
			mGoodNeedCarLength.setText(good.needLength);
			mGoodContact = (TextView) findViewById(R.id.good_contact);
			mGoodContact.setText(good.contact);
			mGoodPhone = (TextView) findViewById(R.id.good_phone);
			mGoodPhone.setText(good.phone);
			mGoodStartTime = (TextView) findViewById(R.id.good_start_time);
			mGoodStartTime.setText(good.startDate);
			mGoodThirdPay = (CheckBox) findViewById(R.id.good_third_pay);

			mOrderSendContact = (EditText) findViewById(R.id.order_send_contact);
			mOrderSendPhone = (EditText) findViewById(R.id.order_send_phone);
			mOrderSendAddress = (EditText) findViewById(R.id.order_send_address);
			mOrderReceiveContact = (EditText) findViewById(R.id.order_receive_contact);
			mOrderReceivePhone = (EditText) findViewById(R.id.order_receive_phone);
			mOrderReceiveAddress = (EditText) findViewById(R.id.order_receive_address);
			mOrderGoodTrueName = (EditText) findViewById(R.id.order_good_true_name);
			mOrderGoodType = (EditText) findViewById(R.id.order_good_type);
			mOrderGoodWrapMethod = (EditText) findViewById(R.id.order_good_wrap_method);
			mOrderWeigth = (EditText) findViewById(R.id.order_weight);
			mOrderPrice = (EditText) findViewById(R.id.order_price);
			mOrderTimeLimit = (EditText) findViewById(R.id.order_time_limit);
			mOrderContact = (EditText) findViewById(R.id.order_contact);
			mOrderAddress = (EditText) findViewById(R.id.order_address);
			mOrderRemark = (EditText) findViewById(R.id.order_remark);

			// mCarCode = (TextView) findViewById(R.id.car_code);
			// mCarCode.setText(car.carCode);
			// mCarType = (TextView) findViewById(R.id.car_type);
			// mCarType.setText(car.carType);
			// mCarDriverName = (TextView) findViewById(R.id.car_driver_name);
			// mCarDriverName.setText(car.driverName);
			// mCarOwnerName = (TextView) findViewById(R.id.car_driver_name);
			// mCarOwnerName.setText(car.ownerName);
			// mCarContactPhone = (TextView)
			// findViewById(R.id.car_contact_phone);
			// mCarContactPhone.setText(car.driverPhone);
			// mCarScore = (TextView) findViewById(R.id.car_score);
			// mCarScore.setText(car.creditScore);
			// mCarWeight = (TextView) findViewById(R.id.car_weight);
			// mCarWeight.setText(car.carWeigth);
			// mCarLength = (TextView) findViewById(R.id.car_length);
			// mCarLength.setText(car.carLength);
			// mCarCurrentPlace = (TextView) findViewById(R.id.current_place);
			// mCarCurrentPlace.setText(car.currentPlace);
			mCarThirdPay = (CheckBox) findViewById(R.id.car_third_pay);
		} catch (Exception e) {
		}

		mOrderUseRight = (CheckBox) findViewById(R.id.order_use_right);
		mOrderUseGuarantee = (CheckBox) findViewById(R.id.order_use_guarantee);
		mConfirm = (Button) findViewById(R.id.confirm);
		mConfirm.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirm:
			final String truckID = MyApp.sCarInfo.id;
			final String ShipperContactPerson = mOrderSendContact.getText()
					.toString();
			final String ShipperTel = mOrderSendPhone.getText().toString();
			final String ShipperAddress = mOrderSendAddress.getText()
					.toString();
			final String Consignee = "";
			final String ConsigneeContactPerson = mOrderReceiveContact
					.getText().toString();
			final String ConsigneeTel = mOrderReceivePhone.getText().toString();
			final String ConsigneeAddress = mOrderReceiveAddress.getText()
					.toString();
			final String CargosDescription = mOrderGoodTrueName.getText()
					.toString();
			final String CargosNature = mOrderGoodType.getText().toString();
			final String Packing = mOrderGoodWrapMethod.getText().toString();
			final String Quantity = "";
			final String Weight = mOrderWeigth.getText().toString();
			final String Value = mOrderPrice.getText().toString();
			final String TimeLimits = mOrderTimeLimit.getText().toString();
			final String ShippingCost = "";
			final String Merchandiser = mOrderContact.getText().toString();
			final String MerchandiserTel = "";
			final String Remarks = mOrderRemark.getText().toString();
			final boolean useNotarize = mOrderUseRight.isChecked();
			final boolean useShippingCostGuarantee = mOrderUseGuarantee
					.isChecked();

			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				@Override
				protected void onPreExecute() {
					mDialog = ProgressDialog.show(NewOrder.this, "", "发布中...");
				};

				@Override
				protected void onPostExecute(Object result) {
					mDialog.dismiss();
					if (result instanceof Exception) {
						Exception e = (Exception) result;
						Toast.makeText(NewOrder.this, "发布失败，请稍候重试",
								Toast.LENGTH_LONG).show();
					} else if (result instanceof SoapPrimitive) {
						SoapPrimitive sp = (SoapPrimitive) result;
						try {
							Integer.valueOf(sp.toString());

							Toast.makeText(NewOrder.this, "发布成功",
									Toast.LENGTH_LONG).show();

							NewOrder.this.finish();
						} catch (Exception e) {
							Toast.makeText(NewOrder.this, sp.toString(),
									Toast.LENGTH_LONG).show();
						}
					} else {
					}
				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient.companyCreateOrder(
								mCertificate, truckID, MyApp.sGoodInfo.id,
								MyApp.sGoodInfo.company, ShipperContactPerson,
								ShipperTel, ShipperAddress, Consignee,
								ConsigneeContactPerson, ConsigneeTel,
								ConsigneeAddress, CargosDescription,
								CargosNature, Packing, Quantity, Weight, Value,
								TimeLimits, ShippingCost, Merchandiser,
								MerchandiserTel, Remarks, useNotarize,
								useShippingCostGuarantee);
					} catch (IOException e) {
						e.printStackTrace();
						return e;
					} catch (XmlPullParserException e) {
						e.printStackTrace();
						return e;
					}
				}
			}.execute();
			break;
		}
	}
}
